<?php

namespace System\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Session\Container;
use System\Model\Catalog;
use System\Model\Utility;
use System\Model\Products;
use System\Model\Actionimg;

class ProductController extends AbstractActionController {

    private function checkLogin() {
        $session = new Container("ssadmin");
        if ($session->adminusername == null) {
            $this->redirect()->toUrl(WEB_PATH . "/cpadmin");
        }
    }

    private $CatalogTable;
    private $ProductsTable;
    private $ImagesTable;

    private function getCatalogTable() {
        if (!$this->CatalogTable) {
            $pst = $this->getServiceLocator();
            $this->CatalogTable = $pst->get("System\Model\CatalogTable");
        }
        return $this->CatalogTable;
    }

    private function getProductsTable() {
        if (!$this->ProductsTable) {
            $pst = $this->getServiceLocator();
            $this->ProductsTable = $pst->get("System\Model\ProductsTable");
        }
        return $this->ProductsTable;
    }

    private function getImagesTable() {
        if (!$this->ImagesTable) {
            $pst = $this->getServiceLocator();
            $this->ImagesTable = $pst->get("System\Model\ImagesTable");
        }
        return $this->ImagesTable;
    }

    public function indexAction() {
        $this->checkLogin();
        $this->layout("layout/layoutadmin");
    }

    public function addproductAction() {
        $action = $this->params()->fromPost('btn');
        if ($action != null) {
            $this->addpro();
        }
        $this->checkLogin();
        $this->layout("layout/layoutadmin");
        $listcatalog = $this->getCatalogTable()->loadcatalog(-1);
        return array("listcatalog" => $listcatalog);
    }

    private function addpro() {
         $objUl = new Utility();
        $productName = addslashes(trim($objUl->removescript($this->params()->fromPost("productname"))));
        $productLink = $this->getProductsTable()->checkurlproduct($objUl->chuyenDoi($productName));
        $desProduct = addslashes(trim($objUl->removescript($this->params()->fromPost("desproduct"))));
        $priceProduct = addslashes(trim($objUl->removescript($this->params()->fromPost("price"))));
        $quanlityProduct = addslashes(trim($objUl->removescript($this->params()->fromPost("quanlity"))));
        $xuatxuProduct = addslashes(trim($objUl->removescript($this->params()->fromPost("xuatxu"))));
        $baohanhProduct = addslashes(trim($objUl->removescript($this->params()->fromPost("baohanh"))));
        $catalogProduct = addslashes(trim($objUl->removescript($this->params()->fromPost("catalog"))));
        $content = $this->params()->fromPost('content');
        $listNameImg = $_FILES["image"]["name"];
        $array_temp_name = $_FILES['image']['tmp_name'];
        $data = array();
        $data["productName"] = $productName;
        $data["productLink"] = $productLink;
        $data["productDescription"] = $desProduct;
        $data["productContent"] = $content;
        $data["productPrice"] = $priceProduct;
        $data["productQuanlity"] = $quanlityProduct;
        $data["productXuatXu"] = $xuatxuProduct;
        $data["productBaoHanh"] = $baohanhProduct;
        $data["productUploadDate"] = Date("Y:m:d");
        $data["productUpdateDate"] = Date("Y:m:d");
        $data["catalogId"] = $catalogProduct;
        $data["productActive"] = true;
        $data["productStart"] = 0;
        $data["productSaleOff"] = 0;
        $objProduct = new Products();
        $objProduct->exchangeArray($data);
        $idProduct = $this->getProductsTable()->addproduct($objProduct);
        $objImg = new Actionimg();
        if ($idProduct != null) {
            $dir = Date("d-m-Y");
            $dirpath = str_replace("\\", "/", APPLICATION_PATH . "/public/imtProduct/" . $dir . "/");
            $dirpaththumb = str_replace("\\", "/", APPLICATION_PATH . "/public/imtProduct/" . $dir . "/thumb/");
            $dirpaththumbdetail = str_replace("\\", "/", APPLICATION_PATH . "/public/imtProduct/" . $dir . "/thumbdetail/");
            if (!file_exists($dirpath) && !is_dir($dirpath)) {
                mkdir($dirpath);
                mkdir($dirpaththumb);
                mkdir($dirpaththumbdetail);
            }
            $limit = 0;
            for ($i = 0; $i < count($listNameImg); $i++) {
                $ext = substr(strrchr($listNameImg[$i], '.'), 1);
                $fileimgnew = time() . $limit . "." . $ext;
                copy($array_temp_name[$i], $dirpath . $fileimgnew);
                copy($array_temp_name[$i], $dirpaththumb . $fileimgnew);
                copy($array_temp_name[$i], $dirpaththumbdetail . $fileimgnew);
                @$objImg->croppimgpro($dirpaththumbdetail . $fileimgnew, 70, 70);
                @$objImg->croppimgpro($dirpaththumb . $fileimgnew, 150, 100);
                $data = array();
                $data["productId"] = $idProduct;
                $data["img_Url"] = "imtProduct/" . $dir . "/" . $fileimgnew;
                $data["img_thumb"] = "imtProduct/" . $dir . "/thumb/" . $fileimgnew;
                $data["img_Date"] = Date("Y:m:d");
                $data["imt_Dir"] = $dir;
                $data["img_thumbdetail"] = "imtProduct/" . $dir . "/thumbdetail/" . $fileimgnew;
                $this->getImagesTable()->addimg($data);
                $limit++;
            }
        }
        echo 'addok*duypc*Thêm sản phẩm thành công!';
        die;
    }

}
